package cn.edu.sdju.fengguoyu.ahoedgemonitor;

import cn.edu.sdju.fengguoyu.po.ServiceInfoTableRow;
import cn.edu.sdju.fengguoyu.util.AhoUtil;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.util.Pair;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * JavaFX App
 */
public class App extends Application {
    public int id = -1;
    public static App app;
    public static TextArea logTextArea;

    public static String defaultDNS = "dns8.hichina.com";
    public RPCThread rpcThread = null;
    public static String localIPv4Address;
    public HashMap<Sprite, UserInfoTableRow> sprite2UserInfoTable = new HashMap<>();

    public static App getApp() {
        return app;
    }
    ArrayList<Sprite> sprites = new ArrayList<>();
    ArrayList<Pair<Sprite,Sprite>> connections = new ArrayList<>();
    HashMap<Sprite,ServiceInfoTableRow> sprite2ServiceInfoTable = new HashMap<>();
    private static Scene scene;
    public static boolean stopDraw = false;
    List<ServiceInfoTableRow> serviceInfoTableRows = new ArrayList<>();
    List<UserInfoTableRow> userInfoTableRows = new ArrayList<>();
    public ObservableList<UserInfoTableRow> userInfoTableRowsObservableList = FXCollections.observableArrayList(userInfoTableRows);

    public ObservableList<ServiceInfoTableRow> serviceInfoTableRowObservableList = FXCollections.observableArrayList(serviceInfoTableRows);
    @Override
    public void start(Stage stage) throws IOException {
        localIPv4Address = AhoUtil.getLocalIPv4Address();
        scene = new Scene(loadFXML("primary"), 820, 620);
        stage.setTitle("AhoEdge监控面板");
        stage.getIcons().add(new Image(new FileInputStream(new File(App.class.getResource("logo.png").getFile()))));
        scene.getStylesheets().add(App.class.getResource("css/main.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
        AhoEdgeMessageListener listener = new AhoEdgeMessageListener();
        listener.setDaemon(true);
        listener.start();
    }

    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }

    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }
    public App() {

    }

    public static void main(String[] args) {
        if (app == null) {
            app = new App();
        }
        launch();
    }

}